﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.SolicitudesPrestamo;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class PrestamosDevoluciones : UserControl, ISolicitudesPrestamoView
    {

        #region fields

        SolicitudesPrestamoPresenter _presenter;

        #endregion

        #region Properties

        public Guid GuidCurrentList
        {
            get { return new Guid(Page.Request.QueryString["lstID"]); }
        }
        public List<SolicitudDePrestamosContentType> listaSolicitud { get; set; }
        public List<SolicitudDePrestamosContentType> listaSolicitudPrestados { get; set; }
        public bool resultEliminarSolicitud { get; set; }
        public int idSolicitud { get; set; }

        public int PageIndex
        {
            get
            {
                return pgrTemplates.PageIndex;
            }
            set
            {
                pgrTemplates.PageIndex = value;
            }
        }

        public int PageSize
        {
            get
            {
                return pgrTemplates.PageSize;
            }
            set
            {
                pgrTemplates.PageSize = value;
            }
        }

        public int TotalRecords
        {
            get
            {
                return pgrTemplates.TotalRecords;
            }
            set
            {
                pgrTemplates.TotalRecords = value;
            }
        }

        #endregion

        #region Form Methods

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new SolicitudesPrestamoPresenter(this);
            pgrTemplates.OnPageChanged += new Pager.PageChangeHandler(pgrTemplates_OnPageChanged);
            pgrTemplates.NumberOfPagesToDisplay = 10;
            PageIndex = 0;
            PageSize = 10;
            pgrTemplates.BuildPager();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter.CargarDevoluciones();
        }

        protected void gvSolicitudes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvSolicitudes_RowCreated(object sender, GridViewRowEventArgs e)
        {
        }

        protected void gvSolicitudes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Controls[0].Visible = false;
            }
        }

        void pgrTemplates_OnPageChanged(object sender, Controls.PageChangeEventArgs e)
        {
            _presenter.CargarDevoluciones();
            pgrTemplates.BuildPager();
        }

        public void DesplegarVentanaModal(string Url, string Titulo, Int32 ancho, Int32 alto, Page pagina)
        {
            StringBuilder scriptModal = new StringBuilder();
            scriptModal.Append("var showMyModalDialog = function() { var options = { ");
            scriptModal.Append("url : '");
            scriptModal.Append(Url);
            scriptModal.Append("', ");
            scriptModal.Append("title: '");
            scriptModal.Append(Titulo);
            scriptModal.Append("', width: ");
            scriptModal.Append(ancho.ToString());
            scriptModal.Append(", height: ");
            scriptModal.Append(alto.ToString());
            scriptModal.Append(", dialogReturnValueCallback: onDialogClose }; SP.UI.ModalDialog.commonModalDialogOpen(" + "'" + Url + "'" + ",options, onDialogClose, null); }; ");
            scriptModal.Append("function onDialogClose(dialogResult, returnValue) { ");
            scriptModal.Append("if (dialogResult == SP.UI.DialogResult.OK) { ");
            scriptModal.Append(" SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); } ");
            scriptModal.Append(" } ");

            pagina.ClientScript.RegisterClientScriptBlock(pagina.GetType(), "Mensaje", scriptModal.ToString(), true);
            pagina.ClientScript.RegisterStartupScript(typeof(Page), "Precarga", "ExecuteOrDelayUntilScriptLoaded(showMyModalDialog, 'sp.ui.dialog.js');", true);
        }

        protected void aceptarRadicadoClick(object sender, EventArgs e)
        {
            ImageButton imageButton = (ImageButton)sender;
            TableCell tableCell = (TableCell)imageButton.Parent;
            GridViewRow row = (GridViewRow)tableCell.Parent;
            DesplegarVentanaModal(SPContext.Current.Web.Url + "/_layouts/MS.InformationManagement.WP/ValidarPassword.aspx?form=Devolucion&ID=" + row.Cells[0].Text + "&lstID=" + GuidCurrentList, "Sistema de Gestión Documental", 600, 260, this.Page);

        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            gvSolicitudes.DataSource = listaSolicitud;
            gvSolicitudes.DataBind();
            
            LoadControl();

        }

        private void LoadControl()
        {
            PrestamosDocumentales pd = (PrestamosDocumentales)this.Parent.Parent;
            if (pd != null)
            {
                pd.SetDevoluciones(listaSolicitud.Count);
            }
        }
        public void resultCancelarSolicitud()
        {

        }

        #endregion
    }
}
